package org.example.service.Impl;

import org.example.dao.UserDao;
import org.example.entity.User;
import org.springframework.stereotype.Service;
import org.example.service.UserService;
import org.example.utils.IdWorker;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    UserDao userDao;

    @Override
    public Boolean addUser(User user) {
        user.setUserId(IdWorker.nextId());
        user.setCreateTime(new Date());
        return userDao.insert(user)>0;
    }

    @Override
    public Boolean deleteUser(Long userId) {
        return userDao.deleteById(userId)>0;
    }

    @Override
    public Boolean updateUser(User user) {
        return userDao.updateById(user)>0;
    }

    @Override
    public User getUser(Long userId) {
        return userDao.selectById(userId);
    }

    @Override
    public User getUserByPhone(Long phone) {
        HashMap<String,Object> map=new HashMap<String, Object>();
        map.put("phone",phone);
        if(userDao.selectByMap(map).size()==0){
            return null;
        }
        return userDao.selectByMap(map).get(0);
    }

    @Override
    public Boolean login(Long phone, String password) {
        User user = getUserByPhone(phone);
        if(user==null){
            return false;
        }
        if(password.equals(user.getPassword())){
            return true;
        }
        return false;
    }

    @Override
    public User getUserByName(String username) {
        HashMap<String,Object> map=new HashMap<String, Object>();
        map.put("username",username);
        if(userDao.selectByMap(map).size()==0){
            return null;
        }
        return userDao.selectByMap(map).get(0);
    }

    @Override
    public List<User> getUsersByList(List<Long> userIds){
        return userDao.selectBatchIds(userIds);
    }
}

